from wtforms import StringField, TextAreaField, IntegerField, ValidationError, widgets, SelectField, \
    SelectMultipleField, DecimalField,FloatField,BooleanField
from wtforms.validators import NumberRange,EqualTo,Email,Regexp
from flask_wtf import FlaskForm
from wtforms.validators import Length, DataRequired
import re

def mobile_validate(value):
    mobile_re = re.compile(
        r'^(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$')
    if not mobile_re.match(value):
        raise ValidationError('手机号码格式错误')


class UserRegForm(FlaskForm):
    username = StringField(label="用户名", validators=[
        DataRequired(message="用户名不能为空"),
        Length(2,30,message="长度2-30位")],
        render_kw={'class': 'form-control', 'placeholder': "请输入用户名"})

    password = StringField(label="密码", validators=[
        DataRequired(message="密码不能为空"),
        Length(2,30,message="长度2-30位")],
        widget=widgets.PasswordInput(),
        render_kw={'class': 'form-control', 'placeholder': "请输入密码"})

    re_password = StringField(label="确认密码", validators=[
        DataRequired(message="确认密码不能为空"),EqualTo("password",message="两次密码输入不一致"),
        Length(2,30,message="长度2-30位")],
        widget=widgets.PasswordInput(),
        render_kw={'class': 'form-control', 'placeholder': "请输入确认密码"})

    email = StringField(label="邮箱", validators=[
        DataRequired(message="邮箱不能为空"),Email(message="邮箱格式不正确")],
        render_kw={'class': 'form-control', 'placeholder': "请输入确认密码"})

    mobile = StringField(label="手机号码", validators=[
        DataRequired(message="手机号码不能为空"),Regexp(r'1[35789]\d{9}',message="手机号码格式不正确")],
        render_kw={'class': 'form-control', 'placeholder': "请输入手机号码"})


class UserLoginForm(FlaskForm):
    username = StringField(label="用户名", validators=[
        DataRequired(message="用户名不能为空"),
        Length(2,30,message="长度2-30位")],
        render_kw={'class': 'form-control', 'placeholder': "请输入用户名"})

    password = StringField(label="密码", validators=[
        DataRequired(message="密码不能为空"),
        Length(2,30,message="长度2-30位")],
        widget=widgets.PasswordInput(),
        render_kw={'class': 'form-control', 'placeholder': "请输入密码"})